package com.maoye.ossp.biz.brand.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.brand.entity.BrandLevel;
import com.maoye.ossp.biz.brand.entity.BrandStyle;
import com.maoye.ossp.biz.brand.service.IBrandStyleService;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:品牌风格定义表 控制器类
 * 开发公司:MOI
 * 开发人员:liuyp2
 * 创建时间:2014-02-12 16:59:38
 */
@Controller
@RequestMapping("biz/brand/style/")
public class BrandStyleController extends BaseController
{
	@Resource
	private IBrandStyleService styleService;
	
	/**
	 * 取得品牌风格定义表分页列表
	 * @param brandLevel
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "list")
	public String list(BrandStyle brandStyle, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		try {
			List<BrandStyle> list = styleService.getAll();
			model.addAttribute("brandStyleList", list);
		} catch (Exception e) {
			e.printStackTrace();
		}//brandLevelService.getAll();
		
		return resultPage("/biz/brand/style/list", model, request);
	}
		
	/**
	 * 添加或更新品牌风格定义表。
	 * @param request
	 * @param response
	 * @param brand 添加或更新的实体
	 * @param bindResult
	 * @param viewName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "save")
	public String save(BrandStyle brandStyle, Model model,
			RedirectAttributes redirectAttributes) 
	{
		try
		{
//			Boolean isExists = brandLevelService.isLevelExists(brandLevel);
//			if(isExists)
//			{
//				addMessage(redirectAttributes, "保存失败：等级名称已存在！");
//				return "/biz/brand/level/add";
//			}
			
			if(brandStyle.getId() == null || brandStyle.getId() == 0)
			{
				brandStyle.setId(idGenerator.getUId());
				styleService.create(brandStyle);
			}
			else
			{
				styleService.update(brandStyle);
			}
			
			addMessage(redirectAttributes, "保存成功");
		}
		catch(Exception e)
		{
			addMessage(redirectAttributes, "保存数据失败");
		}
		
		return "redirect:/biz/brand/style/list";
	}
	
	@RequestMapping(value = "add")
	public String add(BrandLevel brandLevel, Model model) 
	{
		model.addAttribute("brandLevel", brandLevel);
		return "/biz/brand/style/add";
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model) 
	{
		BrandStyle brandStyle = styleService.get(Long.valueOf(id));
		model.addAttribute("brandStyle", brandStyle);
		return "/biz/brand/style/add";
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model) 
	{
		BrandStyle brandStyle = styleService.get(Long.valueOf(id));
		model.addAttribute("brandLevel", brandStyle);
		return "/biz/brand/style/view";
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) 
	{
		styleService.remove(Long.valueOf(id));
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/style/list";
	}
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
		styleService.removeByIds(lAryId);;
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/style/list";
	}
	
}
